import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.codehaus.jackson.util.CharTypes;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.crypto.codec.Base64;

import static com.googlecode.charts4j.Color.*;
import static com.googlecode.charts4j.USAState.Code.*;

import com.googlecode.charts4j.AxisLabels;
import com.googlecode.charts4j.AxisLabelsFactory;
import com.googlecode.charts4j.AxisStyle;
import com.googlecode.charts4j.AxisTextAlignment;
import com.googlecode.charts4j.BarChart;
import com.googlecode.charts4j.BarChartPlot;
import com.googlecode.charts4j.Data;
import com.googlecode.charts4j.Fills;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.GeographicalArea;
import com.googlecode.charts4j.LineChart;
import com.googlecode.charts4j.LinearGradientFill;
import com.googlecode.charts4j.MapChart;
import com.googlecode.charts4j.Plot;
import com.googlecode.charts4j.Plots;
import com.googlecode.charts4j.PoliticalBoundary;
import com.googlecode.charts4j.USAState;



public class ChartMain {
    
    public static void main(String[] args) {
//        Plot plot = Plots.newPlot(Data.newData(0, 66.6, 33.3, 100));
//        LineChart chart = GCharts.newLineChart(plot);
        // EXAMPLE CODE START
//        MapChart chart = GCharts.newMapChart(GeographicalArea.USA);
//
//        PoliticalBoundary al = new USAState( AL, 10);
//        PoliticalBoundary ak = new USAState( AK, 10);
//        PoliticalBoundary az = new USAState( AZ, 10);
//        PoliticalBoundary ar = new USAState( AR, 10);
//        PoliticalBoundary ca = new USAState( CA, 90);
//        PoliticalBoundary co = new USAState( CO, 50);
//        PoliticalBoundary ct = new USAState( CT, 90);
//        PoliticalBoundary de = new USAState( DE, 90);
//        PoliticalBoundary fl = new USAState( FL, 50);
//        PoliticalBoundary ga = new USAState( GA, 10);
//        PoliticalBoundary hi = new USAState( HI, 90);
//        PoliticalBoundary id = new USAState( ID, 10);
//        PoliticalBoundary il = new USAState( IL, 90);
//        PoliticalBoundary in = new USAState( IN, 50);
//        PoliticalBoundary ia = new USAState( IA, 50);
//        PoliticalBoundary ks = new USAState( KS, 10);
//        PoliticalBoundary ky = new USAState( KY, 10);
//        PoliticalBoundary la = new USAState( LA, 10);
//        PoliticalBoundary me = new USAState( ME, 50);
//        PoliticalBoundary md = new USAState( MD, 90);
//        PoliticalBoundary ma = new USAState( MA, 90);
//        PoliticalBoundary mi = new USAState( MI, 90);
//        PoliticalBoundary mn = new USAState( MN, 90);
//        PoliticalBoundary ms = new USAState( MS, 10);
//        PoliticalBoundary mo = new USAState( MO, 10);
//        PoliticalBoundary mt = new USAState( MT, 90);
//        PoliticalBoundary ne = new USAState( NE, 10);
//        PoliticalBoundary nv = new USAState( NV, 90);
//        PoliticalBoundary nh = new USAState( NH, 90);
//        PoliticalBoundary nj = new USAState( NJ, 90);
//        PoliticalBoundary nm = new USAState( NM, 50);
//        PoliticalBoundary ny = new USAState( NY, 90);
//        PoliticalBoundary nc = new USAState( NC, 50);
//        PoliticalBoundary nd = new USAState( ND, 90);
//        PoliticalBoundary oh = new USAState( OH, 90);
//        PoliticalBoundary ok = new USAState( OK, 10);
//        PoliticalBoundary or = new USAState( OR, 90);
//        PoliticalBoundary pa = new USAState( PA, 50);
//        PoliticalBoundary ri = new USAState( RI, 90);
//        PoliticalBoundary sc = new USAState( SC, 50);
//        PoliticalBoundary sd = new USAState( SD, 10);
//        PoliticalBoundary tn = new USAState( TN, 10);
//        PoliticalBoundary tx = new USAState( TX, 10);
//        PoliticalBoundary ut = new USAState( UT, 10);
//        PoliticalBoundary vt = new USAState( VT, 90);
//        PoliticalBoundary va = new USAState( VA, 50);
//        PoliticalBoundary wa = new USAState( WA, 90);
//        PoliticalBoundary wv = new USAState( WV, 10);
//        PoliticalBoundary wi = new USAState( WI, 90);
//        PoliticalBoundary wy = new USAState( WY, 10);
//
//        chart.addPoliticalBoundaries(al,ak,az,ar,ca,co,ct,de,fl,ga,hi,id,il,in,ia,ks,ky,la,me,md,ma,mi,mn,ms,mo,mt,ne,nv,nh,nj,nm,ny,nc,nd,oh,ok,or,pa,ri,sc,sd,tn,tx,ut,vt,va,wa,wv,wi,wy);
//        chart.setColorGradient(WHITE, RED, BLUE);
//        chart.setBackgroundFill(Fills.newSolidFill(ALICEBLUE));
        
        
     // EXAMPLE CODE START
        // Defining data plots.
        BarChartPlot team1 = Plots.newBarChartPlot(Data.newData(25, 43, 12, 30), BLUEVIOLET, "Team A");
        BarChartPlot team2 = Plots.newBarChartPlot(Data.newData(8, 35, 11, 5), ORANGERED, "Team B");
        BarChartPlot team3 = Plots.newBarChartPlot(Data.newData(10, 20, 30, 30), LIMEGREEN, "Team C");

        // Instantiating chart.
//        BarChart chart = GCharts.newBarChart(team1, team2, team3);
        BarChart chart = GCharts.newBarChart(team1);
        // Defining axis info and styles
        AxisStyle axisStyle = AxisStyle.newAxisStyle(BLACK, 13, AxisTextAlignment.CENTER);
        AxisLabels score = AxisLabelsFactory.newAxisLabels("Score", 50.0);
        score.setAxisStyle(axisStyle);
        AxisLabels year = AxisLabelsFactory.newAxisLabels("Year", 50.0);
        year.setAxisStyle(axisStyle);

        // Adding axis info to chart.
        chart.addXAxisLabels(AxisLabelsFactory.newAxisLabels("2002", "2003", "2004", "2005"));
        chart.addYAxisLabels(AxisLabelsFactory.newNumericRangeAxisLabels(0, 100));
        chart.addYAxisLabels(score);
        chart.addXAxisLabels(year);

        chart.setSize(600, 450);
        chart.setBarWidth(100);
        chart.setSpaceWithinGroupsOfBars(20);
        chart.setDataStacked(true);
        chart.setTitle("Team Scores", BLACK, 16);
        chart.setGrid(100, 10, 3, 2);
        chart.setBackgroundFill(Fills.newSolidFill(ALICEBLUE));
        LinearGradientFill fill = Fills.newLinearGradientFill(0, LAVENDER, 100);
        fill.addColorAndOffset(WHITE, 0);
        chart.setAreaFill(fill);
        
        System.out.println(chart.toURLForHTML());
        try {
            displayUrlString(chart.toURLString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private static void displayUrlString(final String urlString) throws IOException{
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(ImageIO.read(new URL(urlString))));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        System.out.println(urlString);
        //Export to image
        URL url = new URL(urlString);
        BufferedImage bufferedImage = ImageIO.read(url);
//        ImageIO.write(Scalr.resize(etotBImage, 150), "jpg", new File(myDir));
        File outputfile = new File("E:/chart.png");
        ImageIO.write(bufferedImage, "png",outputfile);
        
        
        
        MailSenderManagement msm = new MailSenderManagement();
        
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("Testing base64 image");
        simpleMailMessage.setFrom("daviteq.test02@gmail.com");
//        simpleMailMessage.setTo("thanhthi.tran@daviteq.com");
  //      simpleMailMessage.setTo("uy.daonguyen@gmail.com");
  //      simpleMailMessage.setTo("uy.daonguyen@live.com");
        simpleMailMessage.setTo("thanhuy.nguyen@daviteq.com");
        StringBuilder bodyHTML = new StringBuilder();
       
        bodyHTML.append("<div><span>Send Mail image with base64 src</span></div>");
        bodyHTML.append("<br/>");
        File file = new File("E://test.jpg");
        try {
            FileInputStream fin = new FileInputStream(file);
            DataInputStream dis = new DataInputStream(fin);
            byte fileContent[] = new byte[(int)file.length()];
            dis.readFully(fileContent);
            
            //all chars in encoded are guaranteed to be 7-bit ASCII
            byte[] encoded = Base64.encode(fileContent);
            String encodedFile = new String(encoded);
            System.out.println(encodedFile);
//            bodyHTML.append("<img src=\"data:image/jpg;base64,");
//            bodyHTML.append(encodedFile);
//            bodyHTML.append("\">");
//            bodyHTML.append("<br/>");
            bodyHTML.append("<img src=");
            bodyHTML.append(urlString);
            bodyHTML.append(" >");
            bodyHTML.append("<br/>");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bodyHTML.append("<br/>");
        bodyHTML.append("<div><span>End of mail</span></div>");
        simpleMailMessage.setText(bodyHTML.toString());
        
        CustomMailSender customMailSender = new CustomMailSender();
        msm.setMailSender(customMailSender);
        msm.sendMail("Dao Nguyen Thanh Uy", "Body content ",simpleMailMessage);
    }
    
//    public static BufferedImage resizeBufferedImage(BufferedImage image, Scalr.Method scalrMethod, Scalr.Mode scalrMode, int width, int height)  {
//            BufferedImage bi = image;
//            bi = resize( image, scalrMethod, scalrMode, width, height);
//        return bi;
//    }
}
